package com.hifar.test.api.constant;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.Instant;

/**
 * 组织信息验证错误代码枚举
 * 定义组织信息数据验证失败时的详细错误信息
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum OrganizationValidationErrorCode {
    
    // 请求相关错误
    NULL_DATA_REQUEST("ORG_VAL_001", "验证失败：数据请求为空", "请求对象不能为空，请检查请求参数"),
    NULL_DATA_LIST("ORG_VAL_002", "验证失败：数据列表为空", "数据列表不能为空，请检查请求数据"),
    NULL_DATA_ITEM("ORG_VAL_003", "验证失败：数据项为空", "数据项不能为空，请检查数据项内容"),
    
    // 组织信息相关错误
    NULL_ORGANIZATION_INFO("ORG_VAL_004", "验证失败：组织信息为空", "组织信息不能为空，请检查组织信息内容"),
    NULL_ORG_ID("ORG_VAL_005", "验证失败：组织ID为空", "组织ID不能为空，请检查组织ID字段"),
    NULL_DEPT_NAME("ORG_VAL_006", "验证失败：部门名称为空", "部门名称不能为空，请检查部门名称字段"),
    NULL_PARENT_ID("ORG_VAL_007", "验证失败：父级ID为空", "父级ID不能为空，请检查父级ID字段"),
    
    // 字段格式错误
    INVALID_ORG_ID("ORG_VAL_008", "验证失败：组织ID格式错误", "组织ID格式不正确，请检查组织ID格式"),
    INVALID_DEPT_NAME("ORG_VAL_009", "验证失败：部门名称格式错误", "部门名称格式不正确，请检查部门名称格式"),
    INVALID_PARENT_ID("ORG_VAL_010", "验证失败：父级ID格式错误", "父级ID格式不正确，请检查父级ID格式"),
    INVALID_DEPT_CODE("ORG_VAL_011", "验证失败：部门编码格式错误", "部门编码格式不正确，请检查部门编码格式"),
    INVALID_DEPT_LEVEL("ORG_VAL_012", "验证失败：部门级别格式错误", "部门级别格式不正确，请检查部门级别格式"),
    
    // 业务规则错误
    INVALID_DEPT_LEVEL_RANGE("ORG_VAL_013", "验证失败：部门级别超出范围", "部门级别必须在1-10之间，请检查部门级别值"),
    INVALID_SORT_ORDER("ORG_VAL_014", "验证失败：排序字段格式错误", "排序字段必须是数字，请检查排序字段格式"),
    INVALID_STATUS("ORG_VAL_015", "验证失败：状态值错误", "状态值必须是0或1，请检查状态字段值"),
    
    // 日期格式错误
    INVALID_CREATE_DATE("ORG_VAL_016", "验证失败：创建日期格式错误", "创建日期格式不正确，请检查创建日期格式"),
    INVALID_UPDATE_DATE("ORG_VAL_017", "验证失败：更新日期格式错误", "更新日期格式不正确，请检查更新日期格式"),
    
    // 数据完整性错误
    DUPLICATE_ORG_ID("ORG_VAL_018", "验证失败：组织ID重复", "组织ID已存在，请使用唯一的组织ID"),
    INVALID_PARENT_RELATION("ORG_VAL_019", "验证失败：父级关系错误", "父级关系不正确，请检查父级ID是否存在"),
    CIRCULAR_REFERENCE("ORG_VAL_020", "验证失败：循环引用", "存在循环引用，请检查组织层级关系"),
    
    // 系统错误
    VALIDATION_SYSTEM_ERROR("ORG_VAL_021", "验证失败：系统错误", "验证过程中发生系统错误，请联系管理员");
    
    private final String code;
    private final String message;
    private final String suggestion;
    
    OrganizationValidationErrorCode(String code, String message, String suggestion) {
        this.code = code;
        this.message = message;
        this.suggestion = suggestion;
    }
    
    public String getCode() {
        return code;
    }
    
    public String getMessage() {
        return message;
    }
    
    public String getSuggestion() {
        return suggestion;
    }
    
    public OrganizationValidationErrorResponse createErrorResponse() {
        return new OrganizationValidationErrorResponse(this.code, this.message, this.suggestion);
    }
    
    public OrganizationValidationErrorResponse createErrorResponse(int index, String fieldName, Object fieldValue) {
        return new OrganizationValidationErrorResponse(this.code, this.message, this.suggestion, index, fieldName, fieldValue);
    }
    
    /**
     * 组织信息验证错误响应类
     */
    public static class OrganizationValidationErrorResponse {
        @JsonProperty("code")
        private String code;
        @JsonProperty("message")
        private String message;
        @JsonProperty("suggestion")
        private String suggestion;
        @JsonProperty("index")
        private Integer index;
        @JsonProperty("fieldName")
        private String fieldName;
        @JsonProperty("fieldValue")
        private Object fieldValue;
        @JsonProperty("timestamp")
        private long timestamp;
        
        public OrganizationValidationErrorResponse(String code, String message, String suggestion) {
            this.code = code;
            this.message = message;
            this.suggestion = suggestion;
            this.timestamp = Instant.now().toEpochMilli();
        }
        
        public OrganizationValidationErrorResponse(String code, String message, String suggestion, 
                                                 int index, String fieldName, Object fieldValue) {
            this.code = code;
            this.message = message;
            this.suggestion = suggestion;
            this.index = index;
            this.fieldName = fieldName;
            this.fieldValue = fieldValue;
            this.timestamp = Instant.now().toEpochMilli();
        }
        
        // Getters
        public String getCode() { return code; }
        public String getMessage() { return message; }
        public String getSuggestion() { return suggestion; }
        public Integer getIndex() { return index; }
        public String getFieldName() { return fieldName; }
        public Object getFieldValue() { return fieldValue; }
        public long getTimestamp() { return timestamp; }
        
        // Setters (optional, for deserialization)
        public void setCode(String code) { this.code = code; }
        public void setMessage(String message) { this.message = message; }
        public void setSuggestion(String suggestion) { this.suggestion = suggestion; }
        public void setIndex(Integer index) { this.index = index; }
        public void setFieldName(String fieldName) { this.fieldName = fieldName; }
        public void setFieldValue(Object fieldValue) { this.fieldValue = fieldValue; }
        public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
    }
}
